<template>
    <div v-cloak>
        <template>
        <div class="align-right">
              <el-form :inline="true" :model="listQuery">
                  <div class="pull-right">
                    <el-form-item>
                        <el-input
                            placeholder="请输入标签名称"
                            clearable
                            v-model="listQuery.name">
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                      <el-button type="primary" @click="handleSearch" icon="el-icon-search" >搜索</el-button>
                    </el-form-item>
                  </div>
                <div>
                    <el-button  @click="addOne">新增一级标签</el-button>
                    <el-button  @click="addTwo">新增二级标签</el-button>
                </div>
              </el-form>
        </div>
        <el-table
            ref="tagTable"
            :data="tagTables"
            row-key="id"
            border
            highlight-current-row
            style="width: 100%">
                <el-table-column align="center" label="序号" width ="100" >
                    <template slot-scope="scope">
                        <span >{{ scope.$index+1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="标签分类" min-width ="120" >
                    <template slot-scope="scope">
                        <span >{{ scope.row.firstNM }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="标签名称" min-width ="120" >
                    <template slot-scope="scope">
                        <span >{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    align="center"
                    width="200px"
                    label="操作"
                    >
                    <template slot-scope="scope">
                        <div class="default">
                            <el-button type="text"  @click="edit(scope)">编辑</el-button>
                            <el-button type="text" @click="remove(scope)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
        </el-table>
        <el-col :span="24" class="pagination-warp" v-if="tagTables.length>0">
            <div class="text-center">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.currentPage"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.size"
                    layout="total, sizes, prev, pager, next ,jumper"
                    :total="total">
                </el-pagination>
            </div>
        </el-col>
        </template>
        <template>
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogVisible" :before-close="cancel">
            <el-form style="width:70%;margin:0px auto" ref="form" :model="form" label-width="80px">
                <el-form-item prop="pid" label="一级标签" v-if="dialogStatus === 'two'"  :rules="{ required: true, message: '必选项', trigger: 'change' }">
                    <el-col :span="24">
                    <el-select v-model="form.pid" placeholder="请选择" style="width:100%">
                        <el-option
                        v-for="item in options"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                        </el-option>
                    </el-select>
                    </el-col>
                </el-form-item>
                 <el-form-item prop="name" :label="dialogStatus==='two'?'二级标签':'一级标签'" :rules="{ required: true, message: '名称不能为空', trigger: 'blur'}">
                    <el-col :span="24">
                        <el-input v-model.trim="form.name" style="width:100%"></el-input>
                        <el-input v-model.trim="form.id" type="hidden"></el-input>
                    </el-col>
                 </el-form-item>
            </el-form>
             <div slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <el-button type="primary" @click="submit()">确 定</el-button>
            </div>
        </el-dialog>
        </template>
     </div>
</template>
<script>
import { getTags, insertTag, updateTag,　delTag, getChildTags} from '@/api/sys/tag'
export default {
    data() {
        return {
        　　 dialogStatus:null,
            dialogVisible: false,
            form:{
                id:undefined,
                pid: undefined,
                name: undefined
            },
            textMap:{
               one:'新增一级标签',
               two:'新增二级标签'
            },
            total:null,
            tagTables:[],
            options: [],
            listQuery: {
                currentPage: 1,
                size: 20
            }
        }
    },
    created(){
        this.getFirstTags()
        this.getTableList()
    },
    methods: {
        getTableList(){
            getTags(this.listQuery).then(response => {
                if(response.code === 0){     
                    this.tagTables = response.data.records
                    this.total = response.data.total
                }
            })
        },
        getFirstTags(){
            const params = {pid:0}
            getChildTags(params).then(response => {
                if(response.code === 0){
                   this.options = response.data
                }
            })
        },
        handleSearch(){

        },
        handleSizeChange(val) {
            this.listQuery.currentPage = 1;
            this.listQuery.size = val;
            this.getTableList();
        },
        handleCurrentChange(val) {
            this.listQuery.currentPage = val;
            this.getTableList();
        },
        addOne(){
            this.dialogStatus = 'one'
            this.dialogVisible = true
        },
        addTwo(){
            this.dialogStatus = 'two'
            this.dialogVisible = true
        },
        edit(scope) {
            const row =scope.row
            this.form = Object.assign({}, this.form, row)
            this.dialogStatus = 'two'
            this.dialogVisible = true
        },
        cancel(){
            const set = this.$refs
            this.form = {}
            set['form'].clearValidate()
            this.dialogVisible = false
        },
        submit(){
            this.$refs.form.validate((valid) => {
                if (valid) {
                    if(this.form.id === undefined || this.form.id === ''){
                        insertTag(this.form).then(response => {
                            if(response.code === 0){　
                                this.cancel()
                                this.$message({
                                    type: 'success',
                                    message: response.msg
                                })
                                this.getTableList()
                            }
                        })
                    }else{
                        updateTag(this.form).then(response => {
                            if(response.code === 0){　
                                this.cancel()
                                this.$message({
                                    type: 'success',
                                    message: response.msg
                                })
                                this.getTableList()
                            }
                        })
                    }
                    
                } 
            })  
        },
        remove(scope){
            const row = scope.row
             this.$confirm('此操作将删除该标签, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delTag(row).then(response => {
                    if(response.code === 0){　
                        this.cancel()
                        this.$message({
                            type: 'success',
                            message: response.msg
                            })
                    }
                })
            }).catch(() => {
                   
            })
            
        }
    }
}
</script>